package zhoufu.sz.sanxiao_check.presents;

import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.event.Event;
import zhoufu.sz.sanxiao_check.event.RxBus;
import zhoufu.sz.sanxiao_check.models.IDModel;
import zhoufu.sz.sanxiao_check.models.StreetInfoModel;
import zhoufu.sz.sanxiao_check.net.BasicRequestRepository;
import zhoufu.sz.sanxiao_check.net.RequestHelper;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;
import zhoufu.sz.sanxiao_check.utils.TextUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/7/29
 */
public class PlaceChangePresenter implements BasePresent
{

    private BasicRequestRepository mRepository;

    private BasicMsgItem2Contract.View view;

    @Inject
    public PlaceChangePresenter(BasicRequestRepository mRepository)
    {
        this.mRepository = mRepository;
    }

    public void bindView(BasicMsgItem2Contract.View view)
    {
        this.view = view;
    }


    @Override public void onStart()
    {

    }

    @Override public void onPause()
    {

    }

    @Override public void onDestroy()
    {
        mRepository.onDestory();
    }

    public void update(StreetInfoModel model)
    {
        if ( TextUtils.isEmpty(model.getName()) || TextUtils.isEmpty(model.getBusiness())
                || TextUtils
                .isEmpty(model.getHas_Business()) || TextUtils.isEmpty(model.getRun_Name())
                || TextUtils
                .isEmpty(model.getRun_Phone()) )
        {
            view.showToast("请完善所有信息再保存！");
            return;
        }
        {
            mRepository.StreetChange(model, new RequestHelper.RequestCallBack<IDModel>()
            {
                @Override public void onPullStart()
                {
                    view.pullStart();
                }

                @Override public void onPullFailud(Throwable throwable)
                {
                    view.pullFaild();
                }

                @Override public void onPullEnd()
                {
                    view.pullEnd();
                }

                @Override public void onPullSuccessed(IDModel s)
                {
                    if ( s.isSuccess() )
                    {
                        RxBus.getDefault().post(new Event.PlaceChangeRefresh(s.getData().get(0)));
                        view.updateSuccese();
                    } else
                    {
                        view.showToast(s.getErrMsg());
                    }
                }
            });
        }
    }
}
